package com.chushouya.controller.manager.admin;


import com.chushouya.manager.dto.admin.role.RoleListDTO;
import com.chushouya.manager.dto.admin.role.RoleQuery;
import com.chushouya.manager.dto.admin.role.RoleSaveDTO;
import com.chushouya.manager.dto.admin.role.RoleSelectDTO;
import com.chushouya.manager.dto.admin.user.AdminUserListDTO;
import com.chushouya.manager.dto.admin.user.AdminUserQuery;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.lang.Lists;
import com.general.framework.core.schema.PageResult;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.chushouya.common.constant.ServiceType;
import com.chushouya.manager.service.admin.AdminUserService;
import com.chushouya.manager.service.admin.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = ServiceType.PLATFORM + "角色管理")
@RestController
@RequestMapping(ServiceType.PLATFORM + "/role")
public class RoleController {

    @Resource
    private AdminUserService adminUserService;

    @Resource
    private RoleService roleService;

    @ApiOperation("查询角色列表")
    @GetMapping("/list")
    public Response<PageResult<RoleListDTO>> list(RoleQuery query) {
        return Response.success(roleService.selectRolePageResult(query));
    }

    @ApiOperation("新增角色")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Response<Long> add(@Validated @RequestBody RoleSaveDTO role) {
        return Response.success(roleService.addRole(role));
    }

    @ApiOperation("根据角色编号获取详细信息")
    @GetMapping("/getRoleDetail")
    public Response<RoleSaveDTO> getRoleDetail(Long roleId) {
        return Response.success(roleService.getRoleDetail(roleId));
    }

    @ApiOperation("修改保存角色")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Response<Long> edit(@Validated @RequestBody RoleSaveDTO role) {
        return Response.success(roleService.editRole(role));
    }

    @ApiOperation("状态修改")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    public Response<Void> changeStatus(Long roleId, Integer status) {
        roleService.updateRoleStatus(roleId, status);
        return Response.success();
    }

    @ApiOperation("删除角色")
    @Log(businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public Response<Void> remove(Long roleId) {
        roleService.deleteRole(roleId);
        return Response.success();
    }

    @ApiOperation("获取角色选择框列表")
    @GetMapping("/getRoleSelect")
    public Response<List<RoleSelectDTO>> getRoleSelect() {
        return Response.success(roleService.selectRoleSelect());
    }

    @ApiOperation("查询已分配用户角色列表")
    @GetMapping("/authUser/allocatedList")
    public Response<PageResult<AdminUserListDTO>> allocatedList(AdminUserQuery query) {
        return Response.success(adminUserService.selectAllocatedList(query));
    }

    @ApiOperation("查询未分配用户角色列表")
    @GetMapping("/authUser/unAllocatedList")
    public Response<PageResult<AdminUserListDTO>> unAllocatedList(AdminUserQuery query) {
        return Response.success(adminUserService.selectUnAllocatedList(query));
    }
    @ApiOperation("取消授权用户")
    @Log(businessType = BusinessType.GRANT)
    @PostMapping("/authUser/cancel")
    public Response<Void> cancelAuthUser(Long roleId, Long adminUserId) {
        roleService.deleteAuthUser(roleId, Lists.asList(adminUserId));
        return Response.success();
    }

    @ApiOperation("批量取消授权用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色Id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "userIdList", value = "用户IdList", dataType = "List", paramType = "query")
    })
    @Log(businessType = BusinessType.GRANT)
    @PostMapping("/authUser/batchCancel")
    public Response<Void> cancelAuthUserAll(Long roleId, Long[] userIdList) {
        roleService.deleteAuthUser(roleId, Lists.asList(userIdList));
        return Response.success();
    }

    @ApiOperation("批量选择用户授权")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色Id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "userIdList", value = "用户IdList", dataType = "List", paramType = "query")
    })
    @Log(businessType = BusinessType.GRANT)
    @PostMapping("/authUser/selectAll")
    public Response<Void> selectAuthUserAll(Long roleId, Long[] userIdList) {
        roleService.insertAuthUser(roleId, Lists.asList(userIdList));
        return Response.success();
    }

}
